package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.impl.OrderServiceimpl;
import com.service.impl.RaisingServiceimpl;
import com.service.impl.UserRaisingimpl;
import com.service.impl.UserServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Zhengjz
 * @version 1.0
 * @className RaisingController
 * @date 2021/3/1 13:35
 */
@Controller
public class RaisingController {

    @Autowired
    private RaisingServiceimpl raisingServiceimpl;
    @Autowired
    private OrderServiceimpl orderServiceimpl;
    @Autowired
    private UserServiceimpl userServiceimpl;
    @Autowired
    private UserRaisingimpl userRaisingimpl;

//    //购买套餐
//    @RequestMapping("/siyangshilist")
//    public List<Raising> siyangshiList(){
//
//        return raisingServiceimpl.findAllRaising();
//    }

    //饲养时信息展示
    @RequestMapping("/saisinglist")
    public String saisingList(Model model, Integer pageNum, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        String name = request.getParameter("name");
        if (name == null || name == "") {
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            //查询所有的饲养师
            List<Raising> allRaising = raisingServiceimpl.findAllRaising();
            PageInfo pageTeacher = new PageInfo<>(allRaising);
            model.addAttribute("list", pageTeacher);

        }else{
            model.addAttribute("name",name);
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Raising> byName = raisingServiceimpl.findByName(name);
            PageInfo pageTeacher =  new PageInfo<>(byName);
            model.addAttribute("list",pageTeacher);
        }
        return "raising/allclass";
    }

//信息管理--》饲养师信息展示
    @RequestMapping("/raisinginfolist")
    public String raisinglist(Model model,Integer pageNum,HttpServletRequest request){
        if (pageNum == null) {
            pageNum = 1;
        }
        String name = request.getParameter("name");
        if (name == null || name == "") {
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            //查询所有的饲养师
            List<Raising> allRaising = raisingServiceimpl.findAllRaising();
            PageInfo pageTeacher = new PageInfo<>(allRaising);
            model.addAttribute("list", pageTeacher);

        }else{
            model.addAttribute("name",name);
            //当前第几页，每页显示5条数据
            PageHelper.startPage(pageNum, 5);
            List<Raising> byName = raisingServiceimpl.findByName(name);
            PageInfo pageTeacher =  new PageInfo<>(byName);
            model.addAttribute("list",pageTeacher);
        }
        return "raising/raisinglist";
    }

    //处理客户选择的饲养师
    @PostMapping("/xvanzesiyang")
    public void xvanZeSiYang(String siyangshiId, String date, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("饲养师id："+siyangshiId+","+"饲养天数:"+date);
        //获取当前登录用户的id
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());

        //用户选择饲养师寄养时 把用户和寄养师的id绑定到一起
        UserRaising userRaising = new UserRaising();
        userRaising.setUserId(byId.getId());
        userRaising.setUserRasing(Integer.parseInt(siyangshiId));
        //饲养宠物的天数
        userRaising.setPetDate(Integer.parseInt(date));
        //添加用户的id和饲养师的id
        int add = userRaisingimpl.add(userRaising);
//        //添加饲养宠物的天数
//        UserOrder userOrder = new UserOrder();
//        userOrder.setOrderId(Integer.parseInt(siyangshiId));
//        userOrder.setUserId(byId.getId());
        //int i = orderServiceimpl.addTianShu(date);
        response.setContentType("text/html;charset=utf-8");
        if(add == 1){
            response.getWriter().println("选择完成！");
        }else{
            response.getWriter().println("系统内部异常！请联系管理员");
        }

    }


    //跳转购买套餐页面
    @RequestMapping("/gmtaocan")
    public String gmtaocan(String rId,Model model,Integer pageNum){
        if(pageNum == null || pageNum ==0){
            pageNum = 1;
        }
        System.out.println("rid："+rId);
        //当前第几页，每页显示5条数据
        PageHelper.startPage(pageNum, 5);
        List<Order> byId = orderServiceimpl.findById(rId);
        PageInfo pageTeacher = new PageInfo<>(byId);
        model.addAttribute("list",pageTeacher);
        model.addAttribute("rId",rId);
        return "raising/taocan";
    }


    //修改套餐列表
    @PostMapping("/userorder")
    public void userOder(Order order,HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("order："+order);

        //获取当前登录用户的id
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        order.setUserId(byId.getId());
        order.setStatus(0);
        int i = orderServiceimpl.updateOrder(order);
        response.getWriter().println(i);

    }


    //删除饲养师
    @RequestMapping("/rdel")
    public void rfindAll(String id,HttpServletResponse response) throws IOException {
        int rdel = raisingServiceimpl.rdel(id);
        response.getWriter().println(rdel);
    }



    //饲养师的个人信息展示
    @RequestMapping("/gereninfo")
    public String selectInfo(HttpServletRequest request){
        //获取当前登录用户的id
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());

        Raising selinfo = raisingServiceimpl.selinfo(Integer.toString(byId.getId()));
        request.setAttribute("info",selinfo);

        return "raising/siyangshiinfo";
    }

    //饲养师修改个人信息
    @RequestMapping("/xiugai")
    public void xiougai(Raising raising,HttpServletResponse response) throws IOException {
        System.out.println("对象:"+raising);
        //修改信息
        int i = raisingServiceimpl.xiuGaiInfo(raising);
        response.getWriter().println(i);

    }


    //开通饲养师账号
    @RequestMapping("/kaitongsiyang")
    public String kaiTongSiYang(){
        return "raising/siyang";
    }

    //开通
    @PostMapping("/kaitong")
    public void kaitong(User user,Raising raising,HttpServletResponse response) throws IOException {
        System.out.println("duix :"+user);
        System.out.println("duix :"+raising);
        //添加饲养师账号
       userServiceimpl.siyangshifu(user);
       //添加完账号后在用账号查询出账号id
        User user1 = userServiceimpl.selUserId(user.getUserName());

        //查询出用户id之后在添加改用户的角色
        RoleUser roleUser = new RoleUser();
        roleUser.setUserId(user1.getId());
        roleUser.setRoleId(2);
        userServiceimpl.addroleId(roleUser);
        //添加饲养师的个人信息
        raising.setUserId(user1.getId());
        int i = raisingServiceimpl.addSiYang(raising);
        response.getWriter().println(i);
    }


}
